/**
 * 能够很明显的看出线程的并发性
 * 因为连续点击多个子弹的时候，会出现球轮流闪动
 */
package thread;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JFrame;

public class FlyWar extends JFrame {
	public static void main(String[] args) {
		new FlyWar().initGUI();
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<Ball> balls;
	private int R = 800;

	public void initGUI() {
		this.setSize(800, 800);
		this.setBackground(Color.black);
		this.setTitle("线程游戏   缓冲绘图改进");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		balls = new ArrayList<Ball>();

		// setVisible 之后才能够获取画布
		Graphics g = this.getGraphics();
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				super.mousePressed(e);
				balls.add(new Ball(e.getX(), e.getY()));
			}
		});
		
		DrawThread draw = new DrawThread(g, balls, R, R);
		draw.start();
	}

}
